package com.ruoyi.web.controller.system;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.PwsStatus;
import com.ruoyi.system.domain.PwsTable;
import com.ruoyi.system.domain.vo.PwsAndSapAndRgblVo;
import com.ruoyi.system.domain.vo.SapDataRgblVo;
import com.ruoyi.system.service.PwsStatusService;
import com.ruoyi.system.service.PwsTableService;
import com.ruoyi.system.service.SapDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tianfu-li
 * @since 2025-43-04
 */
@RestController
@RequestMapping("/sapData")
@Api(tags = "sap数据")
public class SapDataController {
    @Autowired
    SapDataService sapDataService;
    @Autowired
    PwsStatusService pwsStatusService;
    @Autowired
    PwsTableService pwsTableService;
    @GetMapping("/updateSapData")
    @ApiOperation(value = "根据单号更新配方信息")
    public R updateSapData(@Param("orderNo") String orderNo, @Param("userId") String userId) throws JsonProcessingException {
        boolean flag = sapDataService.updateSapDataBySap(orderNo,userId);
        if(!flag){
            return R.fail("更新失败");
        }else{
            return R.ok();
        }
    }
    @GetMapping("/getSapDataFromOrderNo")
    @ApiOperation(value = "根据单号获取信息")
    public AjaxResult getSapDataFromOrderNo(@Param("orderNo") String orderNo) {
        return AjaxResult.success(sapDataService.selectByOrderNo(orderNo));
    }
    @PostMapping ("/getsapDataByOrderNos")
    @ApiOperation(value = "根据单号批量获取信息" ,notes = "传入单号集合")
    public AjaxResult getsapDataByOrderNos(@RequestBody List<String> orderNos) throws JsonProcessingException {

        List<PwsAndSapAndRgblVo> pwsAndSapAndRgblVos = new ArrayList<>();
        //查询改单号 pws_status表中 fuhe_status 状态 0:未复核，继续执行;1:已复核，返回接口消息:该订单已复核，无法打印。
        if(orderNos.size()>0){
            //1:已复核数据
            List<PwsStatus> pwsStatusList = pwsStatusService.selectByOrderNos(orderNos);
            if(pwsStatusList.size()>0){
                Set<String> orderNoList = pwsStatusList.stream().map(PwsStatus::getOrderNo).collect(Collectors.toSet());
               return AjaxResult.error("单号【"+orderNoList.toString()+"】已复核，无法打印");
                } else{
                 //0:未复核数据
                //获取sap数据更新
                //boolean flag=sapDataService.updateBatchSapDataBySap(orderNos,"");
                //TODO测试用
                boolean flag=true;
                if(!flag) {
                    return AjaxResult.error("更新数据失败");
                }else {
                    //获取sap数据
                    for (String orderNo : orderNos) {
                        PwsAndSapAndRgblVo pwsAndSapAndRgblVo = new PwsAndSapAndRgblVo();
                        //获取sap数据
                        List<SapDataRgblVo> sapDataRgblVoList = sapDataService.selectByOrderNoVo(orderNo);
                        pwsAndSapAndRgblVo.setSapDataRgblVoList(sapDataRgblVoList);
                        //获取pws数据
                        PwsTable pwsTable = pwsTableService.getPwsTableByOrderNo(orderNo);
                        pwsAndSapAndRgblVo.setPwsTable(pwsTable);
                        //添加到集合中
                        pwsAndSapAndRgblVos.add(pwsAndSapAndRgblVo);
                    }
                    if(pwsAndSapAndRgblVos.size()==0){
                        return AjaxResult.error("没有数据");
                    }else {
                        return AjaxResult.success(pwsAndSapAndRgblVos);
                    }
                }


            }

        }else{
            return AjaxResult.error("单号不能为空");
        }

    }


}
